# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# Author     ：skyTree
# version    ：python 3.11
# Description：一些工具方法
"""

from passlib.handlers.pbkdf2 import pbkdf2_sha256


def hash_password(password: str) -> str:
    """
    密码加密
    :param password:
    :return:
    """
    return pbkdf2_sha256.hash(password)


def check_password(password: str, hash_password: str) -> bool:
    """
    密码校验
    :param password:
    :param hash_password:
    :return:
    """
    return pbkdf2_sha256.verify(password, hash_password)


# 将菜单列表转换为树形结构的方法
def build_tree(menus_list, pid=1):
    tree = []
    for m in menus_list:
        if m['pid_id'] == pid:
            m['children'] = build_tree(menus_list, m['id'])
            tree.append(m)
    return tree


def find_children(categories, parent_id=None):
    return [category for category in categories if category['pid_id'] == parent_id]


def build_tree_01(categories, level=1):
    if level == 1:
        roots = [category for category in categories if category['level'] == level]
    else:
        roots = [category for category in categories if category['level'] == level - 1]

    def build_node(node, children=None):
        if children is None:
            children = find_children(categories, node['id'])
        if children:
            node['children'] = [build_node(child) for child in children]
        return node

    # 递归构建树
    tree = [build_node(root) for root in roots]
    return tree
